home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / OOP_Course / Examples / ExtendDraw / DrawView.m < prev    next >
Text File  |  1992-12-19  |  3KB  |  137 lines

  1. #import <appkit/appkit.h>
  2. #import "DrawView.h"
  3. #import "AShape.h"
  4.  
  5. @implementation DrawView
  6.  
  7. -(BOOL)acceptsFirstMouse {return YES;}
  8.  
  9. -initFrame:(NXRect const *)r
  10. {
  11.     [super initFrame:r];
  12.     fill = NO;
  13.     currentShade = 0.0;
  14.     shapeList = [[List alloc] init];
  15.     shapeClassList = [[List alloc] init];
  16.     shapeClass = nil;
  17.     return self;
  18. }
  19.  
  20. -takeShadeFrom:sender
  21. {
  22.     currentShade=[sender floatValue];
  23.     return self;
  24. }
  25.  
  26. -takeFillFrom:sender
  27. {
  28.     fill = [sender selectedTag];
  29.     return self;
  30. }
  31.  
  32. -setDrawType:(int)type
  33. {
  34.     if(type == -1)
  35.         shapeClass = nil;    //If -1, selection arrow was chosen.
  36.     else
  37.         shapeClass = [shapeClassList objectAt:type];  //else a new shape.
  38.     return self;
  39. }
  40.  
  41. -(int)addShapeClass:class
  42. {
  43.     [shapeClassList addObject:class];
  44.     return ([shapeClassList count]-1);  //index of last one added
  45. }
  46.  
  47. -makeNewShape:(NXRect *)bbox
  48. {
  49.     id    newShape;
  50.     if(shapeClass){
  51.         newShape=[[shapeClass alloc] initShapeWithBBox:bbox];
  52.         [newShape setFill:fill];
  53.         [newShape setShade:currentShade];
  54.         return newShape;
  55.     }
  56.     else return nil;
  57. }
  58.  
  59. -addShape:newObject
  60. {
  61.     if (newObject !=nil)
  62.     [shapeList addObject:newObject];
  63.     return newObject;
  64. }
  65.  
  66. -mouseDown:(NXEvent *)e
  67. {
  68.     int oldMask;
  69.     NXRect r2;
  70.     NXPoint pt;
  71.     id aShape;
  72.     
  73.     oldMask = [window addToEventMask:NX_MOUSEDRAGGEDMASK];
  74.     [self lockFocus];
  75.         pt = e->location;
  76.         [self convertPoint:&pt fromView:nil];
  77.         
  78.         /* If selection arrow, then just go select any object moused on. */
  79.         if(shapeClass == nil)
  80.             [self selectMouseAction:&pt];
  81.  
  82.         /* If a drawing tool, then drag out a new shape. */
  83.         else {
  84.             NXSetRect(&r2,pt.x,pt.y,0.0,0.0);
  85.             aShape = [self makeNewShape:&r2];
  86.             if(aShape){
  87.                 [self dragOutShape:aShape startingRect:&r2];
  88.                 [self addShape:aShape];
  89.                 [aShape drawShape];
  90.             }
  91.         }
  92.     [self unlockFocus];
  93.     [window flushWindow];
  94.     [window setEventMask:oldMask];
  95.     return self;
  96. }
  97.  
  98. -selectMouseAction:(NXPoint *)pt
  99. {
  100.     return self;
  101. }
  102.  
  103. -dragOutShape:(id)aShape startingRect:(NXRect *)r2
  104. {
  105.     NXEvent *e;
  106.     NXPoint    pt;
  107.     
  108.     e=[NXApp getNextEvent:NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK];
  109.     while (e->type==NX_MOUSEDRAGGED){
  110.     pt = e->location;
  111.     [self convertPoint:&pt fromView:nil];
  112.     r2->size.width = pt.x - r2->origin.x;
  113.     r2->size.height = pt.y - r2->origin.y;
  114.     [aShape setBbox:r2];
  115.     PSnewinstance();   
  116.     [superview autoscroll:e];
  117.     PSsetinstance(YES);
  118.         if(r2->size.height && r2->size.width)
  119.              [aShape drawShape];
  120.     PSsetinstance(NO);
  121.     e=[NXApp getNextEvent:NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK];
  122.     }
  123.     PSnewinstance();
  124.     return self;
  125. }
  126.  
  127. -drawSelf:(NXRect *)r :(int)c;
  128. {
  129.     int i;
  130.     PSsetgray(NX_WHITE);
  131.     NXRectFill(r); 
  132.     for(i=0; i<[shapeList count]; i++)
  133.         [[shapeList objectAt:i] drawShape];
  134.     return self;
  135. }
  136.  
  137. @end